package com.stars.empoly.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import cn.bingoogolapple.bgabanner.BGABanner
import coil.load
import com.stars.empoly.R
import com.stars.empoly.adapter.HomeClientAdapter
import com.stars.empoly.adapter.HomeServerTypeAdapter
import com.stars.empoly.base.MBFragment
import com.stars.empoly.databinding.FragmentHomeClientBinding
import com.stars.empoly.model.BannerBean
import com.stars.empoly.model.ShopLabel
import com.stars.empoly.vmodel.HomeClientVModel

/**
 * 首页
 *on 2022/9/24
 */
class HomeClientFragment: MBFragment<HomeClientVModel, FragmentHomeClientBinding>()  {


    var adapter:HomeClientAdapter?=null


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.getBanner()
        vm.getInfo()
        //设置搜索条件
        var ss=ArrayList<ShopLabel>()
        ss.add(ShopLabel("0","综合排序", true))//默认添加
        ss.add(ShopLabel("1","价格", false))//默认添加
        ss.add(ShopLabel("2","销量", false))//默认添加
        ss.add(ShopLabel("3","好评度", false))//默认添加

        var adapterType= HomeServerTypeAdapter()
        binding.recyclerType.adapter=adapterType
        adapterType.addData(ss)
        adapterType.setOnItemClickListener { adapterT, view, position ->

            var list = adapterT.data as ArrayList<ShopLabel>

            for ((index, value) in list!!.withIndex()) {
                if (index == position.toInt()) {

                   vm.sortCondition=value.type
                    value.chose = true
                    adapterT?.notifyItemChanged(index)

                    adapter?.clearList()
                    //刷新列表
                    vm.getInfo()
                } else {
                    if(value.chose){
                        value.chose = false
                        adapterT?.notifyItemChanged(index)

                    }

                }

            }
        }

        vm.bannerList.observe(this){
            initBanner(it)
        }



        adapter=HomeClientAdapter(vm)
        binding.recyclerView.adapter=adapter
        vm.bean.observe(this){
            adapter?.clearList()
            adapter?.addData(it.content)
        }




//        binding.starView.setScore(bean.data.evaluate_avg.toFloat())

    }


    override fun listeners() {
        super.listeners()
        adapter?.setOnItemClickListener { adapter, view, position ->

            var item=adapter.getItem(position)
            //todo:


        }
    }

    fun initBanner(bean: List<BannerBean>) {

        var list = ArrayList<String>()

        for (item in bean) {
            list.add(item.image)
        }

        if (list.size > 1) {
            binding.banner.setAutoPlayAble(true)
        } else {
            binding.banner.setAutoPlayAble(false)
        }
        binding.banner.setAdapter(BGABanner.Adapter<LinearLayout, String?> { banner, itemView, model, position -> //图片布局
            val simpleDraweeView = itemView.findViewById<ImageView>(R.id.sdv_item_fresco_content)

            simpleDraweeView.load(model)

        })
        binding.banner.setData(R.layout.item_fresco, list, null)
        binding.banner.setAutoPlayInterval(6000)
        binding.banner.setDelegate { banner, itemView, model, position ->


//            UiSwitch.bundle(context, ArticleDetailActivity::class.java,
//                B().putString("type", "-2")
//                    .putString("content", bean[position].content)
//                    .ok())

        }

    }



    override fun vmClass(): Class<HomeClientVModel> {
        return HomeClientVModel::class.java
    }
    override fun binding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeClientBinding {
        return FragmentHomeClientBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override fun onRefresh() {
        super.onRefresh()
        vm.getInfo()
    }




}
